using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;

namespace tKitchen
{
    public class RecipePresenter
    {
        tKitchenMain domain;
        RecipeInput inputForm;
        Recipe myRecipe;


        public RecipePresenter(tKitchenMain domainIn)
        {
            domain = domainIn;
            myRecipe = new Recipe();
            InitForm();
            inputForm.Categorys.SelectedIndex = -1;
            inputForm.Units.SelectedIndex = -1;
            inputForm.Ingredients.SelectedIndex = -1;
        }

        public RecipePresenter(tKitchenMain domainIn, Recipe recipeIn)
        {
            domain = domainIn;
            

            myRecipe = recipeIn.Copy();

            InitForm();
            if (myRecipe.Category != null)
            {
                inputForm.Categorys.Text = myRecipe.Category.Name;
            }
            else
            {
                inputForm.Categorys.SelectedIndex = -1;
            }
            inputForm.Units.SelectedIndex = -1;
            inputForm.Ingredients.SelectedIndex = -1;
        }



        private void InitForm()
        {
            domain.IngredientAdded += new EventHandler(domain_IngredientAdded);
            domain.IngredientUpdated += new EventHandler(domain_IngredientUpdated);
            domain.UnitAdded += new EventHandler(domain_UnitAdded);
            domain.UnitUpdated += new EventHandler(domain_UnitUpdated);
            domain.RecipeCategoryAdded += new EventHandler(domain_RecipeCategoryAdded);
            domain.RecipeCategoryUpdated += new EventHandler(domain_RecipeCategoryUpdated);


            inputForm = new RecipeInput(this);

            inputForm.SaveClick += new EventHandler(inputForm_SaveClick);
            inputForm.AddIngredientButtonClick += new EventHandler(inputForm_AddIngredientButtonClick);
            inputForm.IngredientEndEdit += new EventHandler(inputForm_IngredientEndEdit);

            inputForm.AddStepButtonClick += new EventHandler(inputForm_AddStepButtonClick);
            inputForm.StepEndEdit += new EventHandler(inputForm_StepEndEdit);




            inputForm.RecipeName = myRecipe.Name;
            inputForm.RecipeDescr = myRecipe.Descr;

            InitIngredientList();
            InitUnitList();
            InitCategoryList();

            PopulateRecipe_IngredintGrid();
            PopulateStepGrid();
        }

        private void InitCategoryList()
        {
            inputForm.Categorys.DataSource = domain.GetRecipeCategoryList();
            inputForm.Categorys.DisplayMember = "Name";
            inputForm.Categorys.ValueMember = "RecipeCategoryId";
        }

        private void InitUnitList()
        {
            inputForm.Units.DataSource = domain.GetUnitList();
            inputForm.Units.DisplayMember = "Name";
            inputForm.Units.ValueMember = "UnitId";
        }

        private void InitIngredientList()
        {
            inputForm.Ingredients.DataSource = domain.GetIngredientList();
            inputForm.Ingredients.DisplayMember = "Name";
            inputForm.Ingredients.ValueMember = "IngredientId";
        }





        #region Event Handlers
        void domain_RecipeCategoryUpdated(object sender, EventArgs e)
        {
            InitCategoryList();
        }

        void domain_RecipeCategoryAdded(object sender, EventArgs e)
        {
            InitCategoryList();
        }

        void domain_UnitUpdated(object sender, EventArgs e)
        {
            InitUnitList();
        }

        void domain_UnitAdded(object sender, EventArgs e)
        {

            InitUnitList();
        }

        void domain_IngredientUpdated(object sender, EventArgs e)
        {
            InitIngredientList();
        }

        void domain_IngredientAdded(object sender, EventArgs e)
        {
            InitIngredientList();
        }

        void inputForm_AddStepButtonClick(object sender, EventArgs e)
        {
            Step temp = new Step();
            temp.Descr = inputForm.StepDescr;
            myRecipe.Steps.Add(temp);
            PopulateStepGrid();
        }
        void inputForm_StepEndEdit(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        void inputForm_AddIngredientButtonClick(object sender, EventArgs e)
        {
            Recipe_Ingredient temp = new Recipe_Ingredient();
            temp.Amount = inputForm.Amount;
            if (inputForm.UnitId != -1)
            {
                temp.Unit = domain.GetUnit(inputForm.UnitId);
            }
            else
            {
                temp.Unit = null;
            }
            temp.Ingredient = domain.GetIngredient(inputForm.IngredientId);

            myRecipe.Ingredients.Add(temp);

            PopulateRecipe_IngredintGrid();
        }
        void inputForm_IngredientEndEdit(object sender, EventArgs e)
        {
            List<Recipe_Ingredient> list = (List<Recipe_Ingredient>)inputForm.Recipe_Ingredients.DataSource;
            int row = ((DataGridViewCellEventArgs)e).RowIndex;
            Recipe_Ingredient temp = list[row];
            domain.UpdateObject(temp);
        }

        void inputForm_SaveClick(object sender, EventArgs e)
        {
            myRecipe.Name = inputForm.RecipeName;
            myRecipe.Descr = inputForm.RecipeDescr;

            if (inputForm.Categorys.SelectedValue != null)
            {
                int catId = (int)inputForm.Categorys.SelectedValue;
                myRecipe.Category = GetRecipeCategory(catId);
            }
            else
            {
                myRecipe.Category = null;
            }

            domain.SaveRecipe(myRecipe);
        }

        #endregion


        private void PopulateRecipe_IngredintGrid()
        {
            List<Recipe_Ingredient> list = (List<Recipe_Ingredient>)myRecipe.Ingredients;
            inputForm.Recipe_Ingredients.DataSource = null;
            inputForm.Recipe_Ingredients.DataSource = list;

        }
        private void PopulateStepGrid()
        {
            inputForm.Steps.DataSource = null;
            inputForm.Steps.DataSource = myRecipe.Steps;
            inputForm.Steps.Refresh();
        }

        internal RecipeCategory GetRecipeCategory(int catId)
        {

            return domain.GetRecipeCategory(catId);
        }

        public void ShowForm()
        {
            inputForm.Show();
        }

        #region To Be Depricated

        internal void AddRecipe_Ingredient(double amountIn, int unitIdIn, int ingIdIn)
        {
            throw new WarningException("This function is to be depricated");
            Recipe_Ingredient temp = new Recipe_Ingredient();
            temp.Amount = amountIn;
            temp.Unit = domain.GetUnit(unitIdIn);
            temp.Ingredient = domain.GetIngredient(ingIdIn);

            myRecipe.Ingredients.Add(temp);


            PopulateRecipe_IngredintGrid();

        }
        internal void AddStep(string descrIn)
        {
            throw new WarningException("This function is to be depricated");
            Step temp = new Step();
            temp.Descr = descrIn;

            //domain.AddObject(temp);

            PopulateStepGrid();

        }


        internal void AddRecipe(Recipe myRecipe)
        {
            throw new WarningException("This function is to be depricated");
            domain.AddObject(myRecipe);

        }

        internal void UpdateRecipe(Recipe myRecipe)
        {
            throw new WarningException("This function is to be depricated");
            domain.UpdateObject(myRecipe);
        }
        internal void SaveRecipe()
        {
            throw new Exception("This function is to be depricated");

            myRecipe.Name = inputForm.RecipeName;
            myRecipe.Descr = inputForm.RecipeDescr;

            int catId = (int)inputForm.Categorys.SelectedValue;
            myRecipe.Category = GetRecipeCategory(catId);

            domain.SaveRecipe(myRecipe);

        }

        #endregion



    }
}
